package com.spboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spboot.entity.Information;
import com.spboot.entity.Like;
import com.spboot.entity.User;
import com.spboot.page.PageInfo;
import com.spboot.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guocb
 * @since 2020-04-27
 */
@Controller
@RequestMapping("/information")
public class InformationController {
    @Autowired
    InformationService informationService ;

    @Autowired
    ModdletableService moddletableService;

    @Autowired
    TypeService typeService;

    @Autowired
    AddressService addressService;

    @Autowired
    ShoppingcartService shoppingcartService;



    /**
     * 首页
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/shouye")
    public String shouye(Model model,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("login");
        if(user!=null){
            //中间悬浮分类
            model.addAttribute("infolist",informationService.list());
            model.addAttribute("typelist",typeService.list());
            //右上购物车查询
            model.addAttribute("shoplist",shoppingcartService.ssum(user.Userid));
            model.addAttribute("select",shoppingcartService.selectall(user.Userid));
            model.addAttribute("shu",shoppingcartService.ssum(user.Userid));
            model.addAttribute("zong",shoppingcartService.zong(user.Userid));
            return  "index";
        }else {
            model.addAttribute("infolist",informationService.list());
            model.addAttribute("typelist",typeService.list());
            return "index";
        }
    }

    /**
     * 商品详情
     * @param model
     * @param informationid 商品id
     * @param request
     * @return
     */
    @RequestMapping("/purchase")
    public String purchase(Model model,Long informationid,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("login");
        if(user!=null){
            Information information = informationService.getById(informationid);
            model.addAttribute("infolist", information);
            model.addAttribute("selectall", informationService.selectall(informationid));
            //地址
            model.addAttribute("address",addressService.saddress(user.Userid));
            //右上购物车查询
            model.addAttribute("shoplist",shoppingcartService.ssum(user.Userid));
            model.addAttribute("select",shoppingcartService.selectall(user.Userid));
            model.addAttribute("shu",shoppingcartService.ssum(user.Userid));
            model.addAttribute("zong",shoppingcartService.zong(user.Userid));
            return "purchase";
        }else {
            Information information = informationService.getById(informationid);
            model.addAttribute("infolist", information);
            model.addAttribute("selectall", informationService.selectall(informationid));
            return "purchase";
        }
    }

    /**
     * 用户订单
     * @param pageno
     * @param model
     * @param informationid
     * @param request
     * @return
     */
    @RequestMapping("/userorder")
    public String order(@RequestParam(defaultValue = "1", required = false) Long pageno, Model model, Long informationid, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("login");
        if(user!=null){
            //右上购物车查询
            model.addAttribute("shoplist",shoppingcartService.ssum(user.Userid));
            model.addAttribute("select",shoppingcartService.selectall(user.Userid));
            model.addAttribute("shu",shoppingcartService.ssum(user.Userid));
            model.addAttribute("zong",shoppingcartService.zong(user.Userid));

        }
        return "order";
    }

    /**
     * 用户订单
     * @param pageno
     * @param model
     * @param informationid
     * @param request
     * @return
     */
    @RequestMapping("/restuserorder")
    @ResponseBody
    public Object restorder(@RequestParam(defaultValue = "1", required = false) Long pageno, Model model, Long informationid, HttpServletRequest request,String realpageno){
        User user = (User) request.getSession().getAttribute("login");
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        PageInfo<Information> pageInfo = new PageInfo<>(pageno, 20);
        pageInfo = (PageInfo<Information>) informationService.selectorder(user.Userid,pageInfo, queryWrapper);
        Map<String,List> map=new HashMap<>();
        List<String> ordernumberlist=informationService.selectordernumber(user.Userid);
        int i=0;
        for (String ordernumber : ordernumberlist) {
            i++;
            List<Information> listi =new ArrayList<>();
            for (Information information : pageInfo.getRecords()){
                if(information.ordernumber.equals(ordernumber)){
                        listi.add(information);
                    }
                }
            map.put(ordernumber,listi);
        }
        return map;
    }
}

